package naga.x.platform2.app;

import java.util.HashMap;
import java.util.Map;

import naga.x.platform2.utils.NumberUtils;
import naga.x.platform2.utils.StringUtils;


/**
 * 渠道
 * @author yang.li
 *
 */
public enum Channel {
	UNKNOWN,
	OFFICIAL("官方", "ofc:"),                // 1
	WEIXIN("微信"),                          // 2
	EGRET("白鹭"),                           // 3
	TELECOM("电信", "tel:"),                 // 4
	SINA("新浪", "sina:"),                   // 5
	YOOH5("游琥"),                           // 6
	IQIYI("爱奇艺", "iqy:"),                 // 7
	QQ_WANBA("QQ玩吧", "qq:"),            // 8
	Q724("7724游戏", "7724:"),               // 9
	J9KGAMES("游戏金榜", "99kg:"),           // 10
	S399("4399", "4399:"),                   // 11
	IAPP_PAY("爱贝支付", "iapppay:"),        // 12
	JG("9G", "9g:"),                         // 13
	ARKFEL("arkfel", "akf:"),                // 14
	QUNHEI("群黑游戏", "qh:"),               // 15
	MEITU("美图游戏", "mt:"),                // 16
	QQ("QQ", "qq:"),                         // 17
	;
	
	public final String name; // lower case
	public final String displayName;
	public final String prefix;
	public boolean debug;
	private static Map<String, Channel> nameMap = new HashMap<String, Channel>(8);
	private static Channel[] vals = values();
	
	static {
		Channel[] vals = Channel.vals;
		for (Channel c : vals) {
			if (c != UNKNOWN) {
				nameMap.put(c.name, c);
			}
		}
	}
	
	Channel() {
		this(null);
	}
	
	Channel(String displayName) {
		this(displayName, "");
	}
	
	Channel(String displayName, String prefix) {
		this.displayName = displayName;
		this.prefix = prefix;
		this.name = name().toLowerCase();
	}
	
	public String withPrefix(String id) {
		String prefix = this.prefix;
		return StringUtils.isNotEmpty(prefix) && !id.startsWith(prefix) ? prefix + id : id;
//		return this == SINA && StringUtils.isNotEmpty(id) && !id.startsWith("sina:") ? "sina:" + id : id;
	}
	
	public String withoutPrefix(String id) {
		String prefix = this.prefix;
		return StringUtils.isNotEmpty(prefix) && id.startsWith(prefix) ? id.substring(prefix.length()) : id;
	}
	
	public static final Channel forCode(int code) {
		Channel[] vals = Channel.vals;
		if (code < 0 || code >= vals.length) {
			return UNKNOWN;
		}
		return vals[code];
	}
	
	public static final Channel forCode(String code) {
		return forCode(NumberUtils.parseInt(code));
	}
	
	public static final Channel forName(String name) {
		Channel c = nameMap.get(name.toLowerCase());
		return c != null ? c : UNKNOWN;
	}
	
	public static int length() {
		return vals.length;
	}
	
	public static Channel[] vals() {
		return vals;
	}
	
}
